package com.ocom.payconfig.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.payconfig.EntityOrderReplaceItem;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.GetOrderReplaceListRequest;
import com.ocom.common.request.payconfig.PauseOrderReplaceRequest;
import com.ocom.common.request.payconfig.StartOrderReplaceRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoOrderReplaceItem;
import com.ocom.payconfig.service.OrderReplaceItemService;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OrderReplaceItemServiceImpl extends ServiceImpl<IDaoOrderReplaceItem, EntityOrderReplaceItem> implements OrderReplaceItemService {

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyId", isMaster = MasterSlaveEnum.SLAVE)
    public Result addOrderReplace(Long companyId, EntityOrderReplaceItem orderReplaceItem) {
        orderReplaceItem.setUpdateTime(new Date());
        orderReplaceItem.setOrderPrice(orderReplaceItem.getDishesPrice() * orderReplaceItem.getDateListCount());
        this.save(orderReplaceItem);
        return ResultUtil.success();
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyId", isMaster = MasterSlaveEnum.SLAVE)
    public Result editOrderReplace(Long companyId, EntityOrderReplaceItem orderReplaceItem) {
        EntityOrderReplaceItem entityOrderReplaceItem = this.getById(orderReplaceItem.getItemId());
        if(ObjectUtil.isEmpty(entityOrderReplaceItem)){
            return ResultUtil.error(ResultEnum.Err_4150);
        }
        if (ObjectUtil.isEmpty(orderReplaceItem.getOrderPrice())) {
            orderReplaceItem.setOrderPrice(entityOrderReplaceItem.getOrderPrice());
        }
        if (ObjectUtil.isEmpty(orderReplaceItem.getDateListCount())) {
            orderReplaceItem.setDateListCount(entityOrderReplaceItem.getDateListCount());
        }

        orderReplaceItem.setUpdateTime(new Date());
        orderReplaceItem.setOrderPrice(orderReplaceItem.getDishesPrice() * orderReplaceItem.getDateListCount());
        this.update(orderReplaceItem, Wrappers.lambdaQuery(EntityOrderReplaceItem.class)
                .eq(EntityOrderReplaceItem::getComId, orderReplaceItem.getComId())
                .eq(EntityOrderReplaceItem::getItemId, orderReplaceItem.getItemId())
                .eq(EntityOrderReplaceItem::getStatus, "1")
        );
        return ResultUtil.success();
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyId", isMaster = MasterSlaveEnum.SLAVE)
    public Result getOrderReplaceList(Long companyId, GetOrderReplaceListRequest request) {
        Page<EntityOrderReplaceItem> page = new Page<>();
        page.setSize(ObjectUtil.isNotEmpty(request.getSize())?request.getSize():-1);
        page.setCurrent(ObjectUtil.isNotEmpty(request.getPage())?request.getPage():-1);
        if(ObjectUtil.isNotEmpty(request.getBeginDate())){
            request.setBeginDate(DateUtil.beginOfMonth(request.getBeginDate()));
        }

        if(ObjectUtil.isNotEmpty(request.getEndDate())){
            request.setEndDate(DateUtil.endOfMonth(request.getEndDate()));
        }

        List<String> status = new ArrayList<>();
        if(ObjectUtil.isNotEmpty(request.getStrStatus())){
            status = Arrays.asList(StrUtil.split(request.getStrStatus(), ","));
        }
        page = this.page(
                page,
                Wrappers.lambdaQuery(EntityOrderReplaceItem.class)
                        .eq(EntityOrderReplaceItem::getComId,request.getComId())
                        .ge(ObjectUtil.isNotEmpty(request.getBeginDate()),EntityOrderReplaceItem::getStartDate,request.getBeginDate())
                        .le(ObjectUtil.isNotEmpty(request.getEndDate()),EntityOrderReplaceItem::getStartDate,request.getEndDate())
                        .in(ObjectUtil.isNotEmpty(status),EntityOrderReplaceItem::getStatus,status)
                        .like(ObjectUtil.isNotEmpty(request.getItemName()),EntityOrderReplaceItem::getItemName,request.getItemName())
                        .orderByDesc(EntityOrderReplaceItem::getItemId)
        );
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal()==0?page.getRecords().size():page.getTotal());
        map.put("list",page.getRecords());
        return ResultUtil.success(
                map
        );
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyId", isMaster = MasterSlaveEnum.SLAVE)
    public EntityOrderReplaceItem getById(Long companyId, Integer id) {
        return this.getOne(Wrappers.lambdaQuery(EntityOrderReplaceItem.class).eq(EntityOrderReplaceItem::getComId,companyId).eq(EntityOrderReplaceItem::getItemId,id).eq(EntityOrderReplaceItem::getStatus,1));
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyId", isMaster = MasterSlaveEnum.SLAVE)
    public Result startOrderReplace(Long companyId, StartOrderReplaceRequest request) {
        EntityOrderReplaceItem entityOrderReplaceItem = new EntityOrderReplaceItem();
        entityOrderReplaceItem.setUpdateTime(new Date());
        entityOrderReplaceItem.setStatus(2);
        entityOrderReplaceItem.setUpdateUserid(request.getUserId());

        this.update(entityOrderReplaceItem,Wrappers.lambdaQuery(EntityOrderReplaceItem.class)
                .eq(EntityOrderReplaceItem::getItemId,request.getItemId())
                .in(EntityOrderReplaceItem::getStatus,1,3)
                .eq(EntityOrderReplaceItem::getComId,request.getComId())
        );
        return ResultUtil.success();
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "companyId", isMaster = MasterSlaveEnum.SLAVE)
    public boolean pauseOrderReplace(Long companyId, PauseOrderReplaceRequest request) {
        EntityOrderReplaceItem entityOrderReplaceItem = new EntityOrderReplaceItem();
        entityOrderReplaceItem.setStatus(3);
        entityOrderReplaceItem.setUpdateUserid(request.getUserId());


        return  this.update(entityOrderReplaceItem,
                Wrappers.lambdaQuery(EntityOrderReplaceItem.class)
                        .eq(EntityOrderReplaceItem::getComId,companyId)
                        .eq(EntityOrderReplaceItem::getItemId,request.getItemId())
                        .eq(EntityOrderReplaceItem::getStatus,2));
    }
}
